import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Button-DropdownButton-下拉按钮',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Button-DropdownButton-下拉按钮'),
        ),
          body: Center(
            child:DropdownButtonDemo(),
          )
      ),
    );
  }
}

var selectItemValue = '上海';

class DropdownButtonDemo extends StatelessWidget{
  List<DropdownMenuItem> generateItemList(){
    final List<DropdownMenuItem> items = [];
    final DropdownMenuItem item1 = new DropdownMenuItem(child: Text('北京'),value: '北京',);
    final DropdownMenuItem item2 = new DropdownMenuItem(child: Text('上海'),value: '上海',);
    final DropdownMenuItem item3 = new DropdownMenuItem(child: Text('广州'),value: '广州',);
    final DropdownMenuItem item4 = new DropdownMenuItem(child: Text('深圳'),value: '深圳',);

    items.add(item1);
    items.add(item2);
    items.add(item3);
    items.add(item4);

    return items;
  }

  @override
  Widget build(BuildContext context){
    return DropdownButton(
      hint: Text('请选择一个城市'),
      items:generateItemList(),
      value: selectItemValue,
      //按钮大小
      iconSize: 48.0,
      style: TextStyle(
        color: Colors.green
      ),
      //阴影高度
      elevation: 24,
      //将下拉框设置为跟父容器一样大小
      isExpanded: true,
      onChanged: (T){
        var obj = T;
        // selectItemValue = T;
        print(T);
      },
    );
  }
}